﻿Imports Excel
Imports System.Windows.Forms

Public Class CodeE
    Inherits ExcelCreateBase

    Public Event PageComplete(ByVal page As Integer, ByVal totalPage As Integer)
    Public Event PrintProcess(ByVal content As String)

    'Const pageCount = 53
    Private _endColumnName = "B"

    Public Sub New(ByVal fileName As String)
        MyBase.New(fileName)
    End Sub

    Private Sub PageSetUp()
        With Me.GetCurrentWorkSheet.PageSetup
            .LeftMargin = CentimetersToPoints(0.2)
            .RightMargin = CentimetersToPoints(0.2)
            .TopMargin = CentimetersToPoints(0.2)
            .BottomMargin = CentimetersToPoints(0.2)
            .HeaderMargin = CentimetersToPoints(0.2)
            .FooterMargin = CentimetersToPoints(0.2)
            .CenterHorizontally = True
            .CenterVertically = True
            .Orientation = Excel.XlPageOrientation.xlPortrait
        End With
    End Sub

    Public Sub DoOutData(ByVal dt As System.Data.DataTable)
        '打开Excel
        RaiseEvent PrintProcess("正在打开Excel")
        打开()
        '进行页面设置
        RaiseEvent PrintProcess("正在页面设置")
        PageSetUp()
        '计算页数
        RaiseEvent PrintProcess("正在计算打印页数")
        Dim page As Integer = Fix(dt.Rows.Count / (报表记录条数字 - 1))
        If dt.Rows.Count Mod (报表记录条数字 - 1) <> 0 Then
            page += 1
        End If
        RaiseEvent PrintProcess("共" & page & "页")

        '打印内容
        RaiseEvent PrintProcess("正在打印内容")
        PrintContent(dt)

        RaiseEvent PrintProcess("正在打印图表")
        PrintChart("A1", "B" & dt.Rows.Count)

        RaiseEvent PrintProcess("正在保存......")
        保存()

        RaiseEvent PrintProcess("正在关闭......")
        关闭()

        RaiseEvent PrintProcess("")
    End Sub

    Private Sub PrintContent(ByVal dt As System.Data.DataTable)
        Dim page As Integer = Fix(dt.Rows.Count / (报表记录条数字 - 1))
        If dt.Rows.Count Mod (报表记录条数字 - 1) <> 0 Then
            page += 1
        End If

        Dim sb As New System.Text.StringBuilder
        For index As Integer = 1 To page
            Dim startRow As Integer = (index - 1) * (报表记录条数字 - 1)
            For jdex As Integer = 1 To 报表记录条数字 - 1
                startRow += 1
                If startRow > dt.Rows.Count Then
                    Exit For
                End If

                sb.Append(CStr(dt.Rows(startRow - 1)("语言")).Trim)
                sb.Append(vbTab)

                sb.AppendLine(CStr(dt.Rows(startRow - 1)("条数")).Trim.Replace(vbNewLine, " "))
            Next
        Next

        Dim con As String = sb.ToString.Trim
        If con.Length > 0 Then
            With Me.GetCurrentWorkSheet
                Clipboard.SetDataObject(con)
                .Range("A1").Select()
                .Paste()
            End With
        End If
    End Sub

    Private Sub PrintChart(ByVal startC As String, ByVal endC As String)
        Me.GetCurrentWorkBook.Charts.Add()
        Me.GetCurrentWorkBook.ActiveChart.ChartType = XlChartType.xlColumnClustered
        Me.GetCurrentWorkBook.ActiveChart.SetSourceData(Me.GetCurrentWorkBook.Sheets("Sheet1").Range(startC & ":" & endC), XlRowCol.xlColumns)
        Me.GetCurrentWorkBook.ActiveChart.Location(XlChartLocation.xlLocationAsNewSheet)
        With Me.GetCurrentWorkBook.ActiveChart
            .HasTitle = True
            .ChartTitle.Characters.Text = "代码统计"
            .Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary).HasTitle = False
            .Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary).HasTitle = False
        End With
        Me.GetCurrentWorkBook.ActiveChart.HasLegend = False
        Me.GetCurrentWorkBook.ActiveChart.HasDataTable = False
        Me.GetCurrentWorkBook.ActiveChart.ChartTitle.Select()
        With Me.GetCurrentWorkBook.ActiveChart.ChartTitle.Font
            .Name = "宋体"
            .Size = 16
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = XlUnderlineStyle.xlUnderlineStyleNone
            .ColorIndex = -4105
            .Background = -4105
            .Bold = True
        End With

        Me.GetCurrentWorkBook.ActiveChart.ApplyDataLabels(AutoText:=True, LegendKey:=False, _
        HasLeaderLines:=False, ShowSeriesName:=False, ShowCategoryName:=False, _
        ShowValue:=True, ShowPercentage:=False, ShowBubbleSize:=False)

    End Sub
#Region "报表必须使用"
    Private _报表字体 As String
    Public Overrides Property 报表字体() As String
        Get
            Return _报表字体
        End Get
        Set(ByVal value As String)
            _报表字体 = value
        End Set
    End Property

    Private _报表记录条数字 As Integer
    Public Overrides Property 报表记录条数字() As Integer
        Get
            Return _报表记录条数字
        End Get
        Set(ByVal value As Integer)
            _报表记录条数字 = value
        End Set
    End Property
#End Region

End Class
